package servlet.user;

import com.alibaba.fastjson.JSON;
import pojo.Result;
import pojo.Songs;
import pojo.User;
import service.SongsService;
import service.impl.SongsServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/user/getSongsServlet")
public class GetSongsServlet extends HttpServlet {
    private SongsService songService = new SongsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 假设通过 session 获取当前用户 ID
        User user = (User) request.getSession().getAttribute("user");
        int userId = user.getUserId();
        Result result = null;
        if (userId == 0) {
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            result = Result.error("用户未登录");
            response.getWriter().write(JSON.toJSONString(result));
            return;
        }
        List<Songs> songsList = songService.getSongsByUploaderId(userId);
        result = Result.success(songsList);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(JSON.toJSONString(result));

    }
}